package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MobileOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Map;

/**
 * 预约控制器Controller
 *
 * @author szh
 * @since 2022/11/23 17:31
 */
@Slf4j
@RestController
@RequestMapping("/mobile/order")
public class MobileOrderController {


    //@Autowired
    //private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private MobileOrderService mobileOrderService;

    /**
     * 提交预约信息
     * @return
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody Map<String,String> map, HttpSession session) throws ParseException {

        log.info("前后端连通");

        //设置预约类型
        map.put("orderType", Order.ORDERTYPE_WEIXIN);
        //1.获取前端传递的手机号和验证码
        String telephone = map.get("telephone");
        String newCode = map.get("validateCode");
        log.info("手机号,验证码-->{},{}", telephone, newCode);

        //2.从redis中获取手机号对应的正确的验证码
        //String oldCode = (String) this.redisTemplate.opsForValue().get(telephone);

        //从session中获取手机号对应的正确的验证码
        String oldCode = (String) session.getAttribute(telephone);

        //验证码不正确校验
        if(null==oldCode||!oldCode.equals(newCode)){
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }

        //校验成功进行预约业务处理
        Result result=this.mobileOrderService.submit(map);

        return result;
    }

}
